package com.example.radiobuttondemo.slice;

import com.example.radiobuttondemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.RadioButton;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.TableLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 获取单选按钮实例对象
        RadioButton radioButton1 = (RadioButton) findComponentById(ResourceTable.Id_raido_btn_1);
        RadioButton radioButton2 = (RadioButton) findComponentById(ResourceTable.Id_raido_btn_2);
        RadioButton radioButton3 = (RadioButton) findComponentById(ResourceTable.Id_raido_btn_3);
        // 设置选项标题
        radioButton1.setText("选项1");
        radioButton2.setText("选项2");
        radioButton3.setText("选项3");
        // 设置选中时的标题颜色
        radioButton1.setTextColorOn(Color.RED);
        // 设置未选中时的标题颜色
        radioButton1.setTextColorOff(Color.GRAY);
        // 设置默认选中选项2
        radioButton2.setChecked(true);

        RadioContainer radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_radio_container);
        radioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int index) {
                // 这里可以获取到用户选中的按钮的位置 index
            }
        });
        radioContainer.mark(2);
        radioContainer.setOrientation(Component.HORIZONTAL);
        radioContainer.cancelMarks();
        ToastDialog t = new ToastDialog(getContext());
        t.setText(String.format("%d", radioContainer.getMarkedButtonId()));
        t.show();

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
